package com.ynkbny.util;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zelin.wang
 * @description
 * @date 7/11/2023
 */
public class SearchUtil {

    public static <T> T parseWhereSql(Object objToBeConverted, Class<T> convertedType, String conditionJson) {
        System.out.println("函数 convertedType 被调用。其中，形参 convertedType：" + convertedType);
        String column = new String("");
        QueryWrapper queryWrapper = new QueryWrapper();
        List<ConditionVo> conditionList = JSON.parseArray(conditionJson, ConditionVo.class);
        if (!StringUtils.isEmpty(conditionJson)) {
            if (!conditionList.isEmpty()) {
                for (ConditionVo conditionVo : conditionList) {
                    column = CameltoUnderLine(conditionVo.getColumn());
                    switch (conditionVo.getType()) {
                        case "eq":
                            queryWrapper.eq(column, conditionVo.getValue());
                            break;
                        case "ne":
                            queryWrapper.ne(column, conditionVo.getValue());
                            break;
                        case "like":
                            queryWrapper.like(column, conditionVo.getValue());
                            break;
                        case "likeLeft":
                            queryWrapper.likeLeft(column, conditionVo.getValue());
                            break;
                        case "likeRight":
                            queryWrapper.likeRight(column, conditionVo.getValue());
                            break;
                        case "notLike":
                            queryWrapper.notLike(column, conditionVo.getValue());
                            break;
                        case "gt":
                            queryWrapper.gt(column, conditionVo.getValue());
                            break;
                        case "lt":
                            queryWrapper.lt(column, conditionVo.getValue());
                            break;
                        case "ge":
                            queryWrapper.ge(column, conditionVo.getValue());
                            break;
                        case "le":
                            queryWrapper.le(column, conditionVo.getValue());
                            break;
                        case "isNull":
                            queryWrapper.isNull(column);
                            break;
                        case "isNotNull":
                            queryWrapper.isNotNull(column);
                            break;
                        case "orderBy":
                            switch (conditionVo.getValue()) {
                                case "asc":
                                    queryWrapper.orderByAsc(column);
                                    break;
                                case "desc":
                                    queryWrapper.orderByDesc(column);
                                    break;
                            }
                            break;

                    }
                }
            }
        }
        return (T) queryWrapper;
    }

    private static String CameltoUnderLine(String str) {
        Pattern compile = Pattern.compile("[A-Z]");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            //将匹配到的大写字符转换成小写，并且在前面添加下划线然后添加到缓冲区。group(0)在没有分配组的时候匹配所有符合的
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
